package cas;

import java.util.concurrent.atomic.AtomicInteger;

public class MainTest {

    private static volatile int anInt = 0;
    private static AtomicInteger atomicInt = new AtomicInteger(0);
    private static ThreadLocal<Integer> localInt = new ThreadLocal<>();

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //每个线程让count自增100次
                    for (int i = 0; i < 100; i++) {
                        anInt++;
//                        synchronized (MainTest.class){
//                            anInt++;
//                        }
                    }

                    for (int i = 0; i < 100; i++) {
                        atomicInt.incrementAndGet();
                    }

                    for (int i = 0; i < 100; i++) {
                        Integer integer = localInt.get();
                        if (integer==null){
                            integer = 0;
                        }
                        localInt.set(++integer);
                    }
                    System.out.println(localInt.get());
                }
            }).start();
        }

        try{
            Thread.sleep(3000);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(anInt);
        System.out.println(atomicInt);
    }

}
